package com.zjitc.serviceedu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjitc.common.exception.AuthException;
import com.zjitc.common.utils.RespBean;
import com.zjitc.serviceedu.entity.EduBanner;
import com.zjitc.serviceedu.entity.vo.BannerVo;
import com.zjitc.serviceedu.service.EduBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 赵诚挚
 * @since 2024-09-28
 */
    @RestController
    @RequestMapping("/serviceedu/edu-banner")
    @Api(tags = "轮播图模块")
    public class EduBannerController {
    @InitBinder
    public void init(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }
        @Autowired
        private EduBannerService eduBannerService;

        @PostMapping("addBanner")
        @ApiOperation("添加轮播图")
        public RespBean addBanner(EduBanner eduBanner) {
            boolean result = eduBannerService.save(eduBanner);
            if(!result) {
                throw new AuthException("添加失败");
            }

            return RespBean.success().msg("添加成功");
        }

        @ApiOperation("修改详情查询")
        @GetMapping("getById/{id}")
        public RespBean getById(@PathVariable("id") Long id) {
            EduBanner eduBanner = eduBannerService.getById(id);
            if(eduBanner == null) {
                throw new AuthException("查询失败");
            }

            return RespBean.success().data("data", eduBanner).msg("查询成功");
        }

        @ApiOperation("修改轮播图")
        @PutMapping("updateBanner")
        public RespBean updateBanner(EduBanner eduBanner) {
            boolean result = eduBannerService.updateById(eduBanner);
            if(!result) {
                throw new AuthException("修改失败");
            }

            return RespBean.success().msg("修改成功");
        }

        @ApiOperation("删除轮播图")
        @DeleteMapping("delBanner/{id}")
        public RespBean delBanner(@PathVariable("id") Long id) {
            boolean result = eduBannerService.removeById(id);
            if(!result) {
                throw new AuthException("删除失败");
            }

            return RespBean.success().msg("删除成功");
        }


        @ApiOperation("条件查询")
        @GetMapping("getPageBatchs")
        public RespBean getPageBatchs(BannerVo bannerVo) {

            Page<EduBanner> page = new Page<>(bannerVo.getPage(), bannerVo.getSize());

            QueryWrapper<EduBanner> queryWrapper = new QueryWrapper<>();

            if (!StringUtils.isEmpty(bannerVo.getName())) {
                queryWrapper.like("name", bannerVo.getName());
            }
            if (!StringUtils.isEmpty(bannerVo.getStartDate())) {
                queryWrapper.ge("created_time", bannerVo.getStartDate());
            }
            if (!StringUtils.isEmpty(bannerVo.getEndDate())) {
                queryWrapper.le("created_time", bannerVo.getEndDate());
            }
            queryWrapper.orderByDesc("id");
            eduBannerService.page(page, queryWrapper);

            if (page == null) {
                throw new AuthException("查询失败");
            }

            List<EduBanner> list = page.getRecords();
            Long total = page.getTotal();

            return RespBean.success().data("list", list).data("total", total).msg("查询成功");
        }

}

